package ch.zhaw.swp1.model.message;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import ch.zhaw.swp1.model.message.address.MMSAddress;
import ch.zhaw.swp1.model.message.body.MMSBody;
import ch.zhaw.swp1.model.status.Status;
import ch.zhaw.swp1.model.status.error.MessageAttachmentError;

/**
 * Class representing an MMS message
 * @author Daniel Tiefenauer
 */
public class MMS extends Message {

	/**
	 * Default version
	 */
	private static final long serialVersionUID = 1L;
	private List<File> attachments = new ArrayList<File>();
	
	/************************************************************************************/
	/* Constructors
	/*----------------------------------------------------------------------------------*/
	public MMS(){
		setSender(new MMSAddress());
		setBody(new MMSBody());
	}

	/************************************************************************************/
	/* Methods
	/*----------------------------------------------------------------------------------*/
	@Override
	public void addRecipient(String recipient) {		
		recipients.add(new MMSAddress(recipient));
	}
	
	/************************************************************************************/
	/* START Validation
	/*----------------------------------------------------------------------------------*/
	@Override
	/**
	 * Validation including validation of attachments
	 */
	public List<Status> validate(){
		super.validate();
		validateAttachments();
		return statusList;
	}
	
	/**
	 * Validate attachments list
	 */
	private void validateAttachments(){
		Iterator<File> it = attachments.iterator();
		while(it.hasNext()){
			File file = it.next();
			if(!file.exists()){
				statusList.add(new MessageAttachmentError(file));
			}
		}
	}
	
	/**
	 * Send functionality including attachment processing 
	 */
	public void send(){
		super.send();
		System.out.println("Attachments: ");
		Iterator<File> it = attachments.iterator();
		while(it.hasNext()){
			System.out.println("\t - " + it.next().getName());
		}
	}


	/************************************************************************************/
	/* START Setter/Getter
	/*----------------------------------------------------------------------------------*/
	@Override
	public void setSender(String sender) {
		this.sender = new MMSAddress(sender);
	}
	@Override
	public MessageType getMessageType() {
		return MessageType.MMS;
	}
	public void setAttachments(List<File> attachments){
		Iterator<File> it = attachments.iterator();
		while(it.hasNext()){
			this.attachments.add(it.next());
		}
	}
	/*----------------------------------------------------------------------------------*/
	/* END Setter/Getter
	/************************************************************************************/

}
